package com.fenghongzhang.day002;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private Long time = 0l;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        /**
         * 如果是按的回退键, 提示用户
         */
        if(keyCode == KeyEvent.KEYCODE_BACK){
            /**
             * 两次按下的时候,在两秒之内
             */
            if(System.currentTimeMillis() - time  > 2000){ //1000006 - 10000001 > 2000
                Toast.makeText(this, "在按就推出", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis(); // time = 10000001
                return true;
            }else{
                finish();
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
